package models

import (
	"gorm.io/gorm"
	"order_srv/basic/config"
	"time"
)

// 用户优惠券联表
type UserCoupon struct {
	Id        uint64         `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;" json:"id"`
	UserId    uint64         `gorm:"column:user_id;type:bigint UNSIGNED;comment:用户id;not null;" json:"user_id"`                  // 用户id
	CouponId  uint64         `gorm:"column:coupon_id;type:bigint UNSIGNED;comment:优惠券id;not null;" json:"coupon_id"`             // 优惠券id
	Status    uint8          `gorm:"column:status;type:tinyint UNSIGNED;comment:状态 1-收藏 2-活得;not null;default:0;" json:"status"` // 状态 1-收藏 2-活得
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (uc *UserCoupon) TableName() string {
	return "user_coupon"
}

func (uc *UserCoupon) UserCouponAdd() error {
	return config.DB.Create(&uc).Error
}

func (uc *UserCoupon) GetUserCouponInFo(UserId uint64) error {
	return config.DB.Model(&uc).Where("user_id = ?", UserId).Limit(1).Find(&uc).Error
}

func (uc *UserCoupon) GetUserCouponInfo(UserId uint64, CouponId uint64) error {
	return config.DB.Model(&uc).Where("user_id = ? and coupon_id = ?", UserId, CouponId).Limit(1).Find(&uc).Error
}

func (uc *UserCoupon) GetUserCouponIn(CouponId uint64) error {
	return config.DB.Model(&uc).Where("coupon_id = ?", CouponId).Limit(1).Find(&uc).Error
}
